package com.exmaple.result;

import java.io.Serializable;
import java.util.List;

public class PageRes<T> implements Serializable{
    private static final long serialVersionUID = 1L;

    //总记录数
    private long total;
    //是否成功
    private Boolean success;
    //错误码
    private String code;
    //消息提示
    private String description;
    //结果对象
    private List<T> data;
    //服务器当前时间戳
    private Long millis = System.currentTimeMillis();

    /**
     * 构造器私有化
     */
    private PageRes() {}

    /**
     * 构造器私有化
     */
    private PageRes(Long total,Boolean succuss, String code, String description, List<T> data) {
        this.total=total;
        this.success = succuss;
        this.code = code;
        this.description = description;
        this.data = data;
    }

    //1. 成功只有无返回值，有返回值两种情况
    //2. 成功只有SUCCESS枚举，没有其他ErrorCode枚举
    public static <T> PageRes<T> success() {
        return new PageRes<T>(0L,true,ErrorCode.SUCCESS.getCode(),ErrorCode.SUCCESS.getDescription(),null);
    }

    // xcrj <T> 静态方法 泛型声明
    // <T>泛型声明；Result<T>返回类型，没有指明泛型时仍然需要写T
    public static <T> PageRes<T> success(long total,List<T> data) {
        // new Result<T>创建对象
        return new PageRes<T>(total,true,ErrorCode.SUCCESS.getCode(),ErrorCode.SUCCESS.getDescription(),data);
    }

    //1. 错误必须指定Errorcode，若无法确定具体错误，选择宏观错误
    //2. 错误只允许返回Errorcode，不允许返回数据data
    public static <T> PageRes<T> error(ErrorCode errorCode) {
        return new PageRes<T>(0L,false,errorCode.getCode(),errorCode.getDescription(),null);
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public Long getMillis() {
        return millis;
    }

    public void setMillis(Long millis) {
        this.millis = millis;
    }
}
